$date = Get-Date -Format "yyyyMMdd"
$output = "sfa_" + $date + "_aarch64"
$releaseDir = "../release"

Write-Host "Building Android binary: $output"

$env:CGO_ENABLED = "0"
$env:GOOS = "linux"
$env:GOARCH = "arm64"

try {
    go build -ldflags="-s -w" -o "$output" 2>&1 | Out-String
    if ($LASTEXITCODE -eq 0) {
        Write-Host "Build succeeded: $output" -ForegroundColor Green

        # 创建 release 目录
        if (-Not (Test-Path -Path "$releaseDir")) {
            New-Item -ItemType Directory -Path "$releaseDir"
        }

        # 压缩为 ZIP
        $zipFile = "$output.zip"
        Compress-Archive -Path "$output" -DestinationPath "$zipFile" -Force
        Write-Host "Created ZIP file: $zipFile" -ForegroundColor Green

        # 移动 ZIP 到 release 目录
        if (-Not (Test-Path -Path "$releaseDir")) {
            New-Item -ItemType Directory -Path "$releaseDir"
        }
        Move-Item -Path "$zipFile" -Destination "$releaseDir\" -Force
        Write-Host "Moved zip to $releaseDir" -ForegroundColor Green

        # 清理生成的二进制文件（可选）
        Remove-Item -Path "$output" -Force
    } else {
        Write-Host "Build failed with exit code: $LASTEXITCODE" -ForegroundColor Red
        exit 1
    }
} catch {
    Write-Host "Exception occurred: $_" -ForegroundColor Red
    exit 1
} finally {
    Write-Host "Cleaning up..."
    $env:CGO_ENABLED = ""
    $env:GOOS = ""
    $env:GOARCH = ""
}
